﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class GroupCompareController : System.Web.Mvc.Controller
    {
        //
        // GET: /GroupCompare/

        public ActionResult Index(int uid, string pdate)
        {

            Common.Web.Cookie cookie = new Common.Web.Cookie();
            //   cookie.SetCookie("querydate", pdate, 20);
            ViewBag.groupquerydate = cookie.GetCookie("groupquerydate");
            cookie.SetCookie("groupquerydate", cookie.GetCookie("groupquerydate"), 24);
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            BenefitHelper.Sys.Groups group = new BenefitHelper.Sys.Groups().GetGroup(uid, db);
            ViewBag.group = group;
            if (BenefitHelper.Sys.VisitPrower.HasGroupPrower(operId, uid, db))
            {
                if (pdate.Length > 4)//查询月数据
                {

                    //    ViewBag.querymonth = pdate.Substring(4, 2);
                    ViewBag.querymonth = pdate.Substring(4, 2);
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.pdate = pdate;
                    ViewBag.gid = uid;
                    string s = cookie.GetCookie("querydate");
                    ViewBag.querydate = s;
                    if (new BenefitHelper.Data.Month.GroupMonthWinOrLoseTick().HasSettlemented(uid, db, Convert.ToInt32(pdate.Substring(4, 2)), Convert.ToInt32(pdate.Substring(0, 4)), true))
                    {
                        List<BenefitHelper.Data.Month.GroupMonthWinOrLoseTick> winticks = new BenefitHelper.Data.Month.GroupMonthWinOrLoseTick().GetSortedGroupMonthWinOrLoseTick(uid, db, Convert.ToInt32(pdate.Substring(4, 2)), Convert.ToInt32(pdate.Substring(0, 4)), true);
                        ViewBag.winticks = winticks;
                        List<BenefitHelper.Data.Month.GroupMonthWinOrLoseTick> lostticks = new BenefitHelper.Data.Month.GroupMonthWinOrLoseTick().GetSortedGroupMonthWinOrLoseTick(uid, db, Convert.ToInt32(pdate.Substring(4, 2)), Convert.ToInt32(pdate.Substring(0, 4)), false);
                        ViewBag.lostticks = lostticks;

                        string js = BenefitHelper.Tools.ChartAtManager.MonthAreaBenefitPic(uid, Convert.ToInt32(pdate.Substring(4, 2)), Convert.ToInt32(pdate.Substring(0, 4)), db);
                        ViewBag.js = js;
                    }
                    else
                    {
                        return Redirect("/Error/index/" + uid + "/" + s);
                    }
                }
                else//查询年数据
                {
                    //    ViewBag.querymonth = pdate.Substring(4, 2);
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.pdate = pdate;
                    ViewBag.gid = uid;
                    ViewBag.querymonth = "";

                    string s = cookie.GetCookie("querydate");
                    ViewBag.datequery = cookie.GetCookie("querydate");

                    List<BenefitHelper.Data.Year.GroupYearWinOrLoseTick> winticks = new BenefitHelper.Data.Year.GroupYearWinOrLoseTick().GetSortedGroupYearWinOrLoseTick(uid, db, Convert.ToInt32(pdate.Substring(0, 4)), true);
                    ViewBag.winticks = winticks;
                    List<BenefitHelper.Data.Year.GroupYearWinOrLoseTick> lostticks = new BenefitHelper.Data.Year.GroupYearWinOrLoseTick().GetSortedGroupYearWinOrLoseTick(uid, db, Convert.ToInt32(pdate.Substring(0, 4)), false);
                    ViewBag.lostticks = lostticks;

                    string js = BenefitHelper.Tools.ChartAtManager.YearAreaBenefitPic(uid, Convert.ToInt32(pdate.Substring(0, 4)), db);
                    ViewBag.js = js;

                }


            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }

            return View();
        }

    }
}
